package zzz.icexxx.swing_demo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import cn.hutool.core.date.DateUtil;

public class MyFrame extends JFrame {
    /**
     * 序列化
     */
    private static final long serialVersionUID = 2465248600806440480L;

    private static JLabel label = null;
    private static JButton button = null;

    public MyFrame(String path) {
        init(path);
    }

    protected void init(String path) {
        setTitle("截图");
        setSize(200, 150);
        setLocation(200, 200);
        setLayout(null);
        setAlwaysOnTop(true);
//        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        label = createLabel();
        add(label);
        button = createButton(path);
        add(button);
        this.setVisible(true);
    }

    private JLabel createLabel() {
        JLabel label = new JLabel("截图成功", JLabel.CENTER);
        label.setBounds(210, 80, 180, 150);
        label.setFont(new Font("宋体", Font.BOLD, 30));
        label.setVisible(false);
        return label;
    }

    private static JButton createButton(final String path) {
        final JButton button = new JButton();
        Color color = new Color(88, 230, 125);
        button.setBackground(color);
        button.setFont(new Font("宋体", Font.BOLD, 36));
        button.setText("截图");
        button.setBounds(10, 10, 200, 100);

        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                run(path);
            }
        });
        return button;
    }

    protected static void run(final String path) {
        new Thread() {
            public void run() {
                System.out.println("截图\t" + DateUtil.now());
                Color color1 = new Color(88, 230, 125);
                Color color2 = new Color(88, 125, 230);
                Color backColor = button.getBackground();
                ScreenImageUtil.captureScreen(path);
                if (color1.equals(backColor)) {
                    button.setBackground(color2);
                } else {
                    button.setBackground(color1);
                }
                label.setVisible(true);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                label.setVisible(false);
            }
        }.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    @Override
    protected void processKeyEvent(KeyEvent e) {
        super.processKeyEvent(e);
    }
}
